/*******************************************************************************
 * @(#)Utils.java 2015年6月19日
 * <p>
 * Copyright 2015 emrubik Group Ltd. All rights reserved.
 * EMRubik PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *******************************************************************************/
package com.inossem.emergency.utils.logger;

import android.graphics.Bitmap;


import com.orhanobut.logger.Logger;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;


public class LogUtils {

    public static final String LOG_NAME_CRASHEXCEPTION = "CrashException";// 崩溃日志名称
    public static final String NEW_LINE = "\n\n";
    public static final String TAG_INOSSEM = "INOSSEM";

    public static long getTimeStampFromString(String format, String time) {
        return getDateFromString(format, time).getTime();
    }

    public static Date getDateFromString(String format, String time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format, Locale.getDefault());
        Date date = null;
        try {
            date = dateFormat.parse(time);
        } catch (ParseException e) {
            Logger.e("", e);
        }
        return date;
    }

    public static String getStringDateFromDate(String format, Date date) {
        String result;
        result = new SimpleDateFormat(format, Locale.getDefault()).format(date);
        return result;
    }

    public static String getCurrentStringDate(String format) {
        Calendar calendar = Calendar.getInstance();
        return getStringDateFromDate(format, calendar.getTime());
    }

    public static String exceptionToString(Throwable throwable) {
        if (throwable == null) {
            return "";
        }
        String exceptionMessage = "";
        try {
            StringWriter sw = new StringWriter();
            throwable.printStackTrace(new PrintWriter(sw, true));
            exceptionMessage = sw.toString();
            sw.flush();
            sw.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return exceptionMessage;
    }

    public static File bitmapToFile(String filePath, Bitmap bitmap) {
        File file = new File(filePath);// 将要保存图片的路径
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
            bitmap.compress(Bitmap.CompressFormat.WEBP, 100, bos);
            bos.flush();
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }



}
